package com.ckd.flyingtrip.activity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.viewpager.widget.ViewPager;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.ckd.flyingtrip.R;
import com.ckd.flyingtrip.adapter.MyfragmentViewpageAdapter;
import com.ckd.flyingtrip.adapter.VehicleAdapter;
import com.ckd.flyingtrip.constants.Constants;
import com.ckd.flyingtrip.fragment.Fragment_MyVehicle;
import com.ckd.flyingtrip.javabean.DingdanBean;
import com.ckd.flyingtrip.utils.BcUtils;
import com.ckd.flyingtrip.utils.DataCheck;
import com.ckd.flyingtrip.utils.Tools;
import com.ckd.flyingtrip.utils.VolleyErrorUtils;
import com.ckd.flyingtrip.widge.HorizontalListView;
import com.ckd.flyingtrip.widge.SingleVolleyRequestQueue;
import com.google.gson.Gson;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MyVehicleActivity extends BaseActivity  implements ViewPager.OnPageChangeListener {
    @BindView(R.id.viewpage)
    ViewPager myvirwpager;
    @BindView(R.id.lsitview)
    HorizontalListView listView;
    // 指示标签的横坐标
    private float cursorX = 0;
    // 定义获取所有按钮的宽度数组
    private int[] WidrhArgs;
    // fragment的集合
    private ArrayList<Fragment> list_Fragment;
    // viewpage适配器
    private MyfragmentViewpageAdapter adapter;
    FragmentManager fm = getSupportFragmentManager();
    private VehicleAdapter vehicleAdapter;
    private List<DingdanBean> list = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_myvehicle);
        ButterKnife.bind(this);
        init();
    }
    private void init() {
        Tools.title_info(this, "我的车辆");
        volleyUserInfo();
//        获取标题
        vehicleAdapter = new VehicleAdapter(MyVehicleActivity.this,null,list);
        listView.setAdapter(vehicleAdapter);



    }

    @SuppressLint("ResourceAsColor")
    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        if(positionOffsetPixels==0){
            vehicleAdapter.setChecked(position);
            listView.setSelection(position);
            vehicleAdapter.notifyDataSetChanged();
        }
    }


    @Override
    public void onPageSelected(int position) {

    }

    @Override
    public void onPageScrollStateChanged(int state) {


    }


    // 指示器的跳转，传入当前所处的页面的下标
    public void cursorAnim(int curItem) {
        // 每次调用，就将指示器的横坐标设置为0，即开始的位置
        cursorX = 0;
//        colorInit();
//        ButtonArgs[curItem].setTextColor(Color.parseColor("#FD5B0E"));
    }




    public void volleyUserInfo() {
        list.clear();
        final Gson mGson = new Gson();
        String url = Constants.DINGDANLIST;
        StringRequest request = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                dismisProgressDialog();
                Log.i("我的订单", s);
                //s为请求返回的字符串数据
                try {
                    JSONObject json = new JSONObject(s);
                    String code = json.getString("code");
                    String msg = json.getString("msg");
                    if (code.equals("1")) {
                        JSONArray data = json.getJSONArray("data");
                        for (int i=0;i<data.length();i++) {
                            String jsonObject = data.getString(i);
                            DingdanBean userInfoBean = mGson.fromJson(jsonObject.toString(), DingdanBean.class);
                            list.add(userInfoBean);
                        }
                        vehicleAdapter.notifyDataSetChanged();

//         将fragment放进集合，并初始化适配器
                        list_Fragment = new ArrayList<Fragment>();

                        for (int i =0;i<list.size();i++){
                            list_Fragment.add(new Fragment_MyVehicle(MyVehicleActivity.this,list,i));
                        }
                        adapter = new MyfragmentViewpageAdapter(fm,
                                list_Fragment);
                        myvirwpager.setAdapter(adapter);
                        // viewpage监听事件，重写onPageSelected()方法，实现左右滑动页面
                        myvirwpager.setOnPageChangeListener(MyVehicleActivity.this);


                        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                            @Override
                            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                                vehicleAdapter.setChecked(i);
                                myvirwpager.setCurrentItem(i);
                                listView.setSelection(i);

                                vehicleAdapter.notifyDataSetChanged();
                            }
                        });
                    } else {
                        Log.i("站好","账号");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    dismisProgressDialog();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                dismisProgressDialog();
                VolleyErrorUtils.checkError(volleyError);
            }
        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {//创建请求参数

                HashMap<String, String> map = new HashMap<String, String>();
                JSONObject json_url = new JSONObject();
                try {
                    json_url.put("userId", BcUtils.getUid());
                    json_url.put("userPhone", BcUtils.getUserInfo().getUserPhone());
                    json_url.put("token", BcUtils.getToken());
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                map.put("inputParameter", DataCheck.signCheck(json_url.toString()));
                return map;
            }
        };
        SingleVolleyRequestQueue.getInstance(this).addToRequestQueue(request);
    }


//    @OnClick({R.id.btn_yue, R.id.btn_youhuijia, R.id.btn_jifen, R.id.btn_yajin, R.id.btn_mingxi})
//    public void onClick(View view) {
//        switch (view.getId()) {
//            case R.id.btn_yue:
//                myvirwpager.setCurrentItem(0);
//                cursorAnim(0);
//                break;
//            case R.id.btn_youhuijia:
//                myvirwpager.setCurrentItem(1);
//                cursorAnim(1);
//                break;
//            case R.id.btn_jifen:
//                myvirwpager.setCurrentItem(2);
//                cursorAnim(2);
//                break;
//            case R.id.btn_yajin:
//                myvirwpager.setCurrentItem(3);
//                cursorAnim(3);
//                break;
//            case R.id.btn_mingxi:
//                myvirwpager.setCurrentItem(4);
//                cursorAnim(4);
//                break;
//        }
//    }
}

